/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import Entitys.Persona;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;


/**
 *
 * @author RIVERA
 */
public class PersonaServices {
         EntityManagerFactory emf= Persistence.createEntityManagerFactory("DrugStoreProPU");
         EntityManager em=emf.createEntityManager();
         
         public void create (Persona p){
          em.getTransaction().begin();
          em.persist(p);
          em.getTransaction().commit();
          em.close();
         }
         
         public void delete (Long p){
          em.getTransaction().begin();
          Persona p1=em.find(Persona.class, p);
          if(p1!= null)
          em.remove(p1);
          em.getTransaction().commit();
          em.close();
         }
         
         public List<Persona> ReadAll(int idDrogueria){
            Query q=em.createNamedQuery("Persona.findAll"); 
            q.setParameter("iddrogueria", idDrogueria);
            return q.getResultList();
         }
         
         public Persona readById(Long idp,int idDrogueria){
             Query q= em.createNamedQuery("Persona.findByIdPersona");
             q.setParameter("idPersona", idp);
             q.setParameter("iddrogueria", idDrogueria);
             Persona prs=(Persona) q.getSingleResult();
             return prs;
         }
}
